
package com.halflife.bigblock;

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class BigBlockServer {
    public static void main(String[] args) {
        System.out.println(1);
    }

    @Test
    public void test() {
        System.out.println(1);
    }

    public void server() throws IOException {
        ServerSocketChannel server = ServerSocketChannel.open();
        server.configureBlocking(false);
        Selector selector = Selector.open();
        server.register(selector, SelectionKey.OP_ACCEPT);
        server.bind(new InetSocketAddress("192.168.31.129", 9090));
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4096);
        while (true) {
            int keys = selector.select();
            if (keys > 0) {
                Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
                while (keyIterator.hasNext()) {
                    SelectionKey key = keyIterator.next();
                    keyIterator.remove();
                    if (key.isAcceptable()) {
                        ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        socketChannel.register(selector, SelectionKey.OP_READ);
                    }else if (key.isReadable()) {
                        SocketChannel socketChannel = (SocketChannel) key.channel();
                        int count = socketChannel.read(byteBuffer);
                        if(count>0){

                        } else if (count < 0) {
                            socketChannel.close();
                        }
                    }
                }
            }
        }
    }
}
